package com.sm.honkon.data.bean

import me.hgj.jetpackmvvm.callback.databind.FloatObservableField
import me.hgj.jetpackmvvm.callback.databind.IntObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.absoluteValue

/**
 * 描述　: 账户信息
 */
data class CommonConfig(
    var waterOpen: Boolean = true,
    var shouJuCheck: Boolean = true,
    var jiaoZiCheck: Boolean = true,
    var jieTouCheck: Boolean = false,
    var tempCheck: Boolean = true,
    var psOrNs: IntObservableField = IntObservableField(0b0001),//ps:0001 ns 0010 ns+ps 0100 shouchi 1000
    var pimiao300: Boolean = true,//0:300ps 1:500ps
    var namiao2: Boolean = true,//0:2ns 1:3ns
    var logoPath: String = "",
    var highestHz: IntObservableField = IntObservableField(0),
    var minVoltage: IntObservableField = IntObservableField(300),
    var minTemp: IntObservableField = IntObservableField(22),
    var maxVoltage: IntObservableField = IntObservableField(800),
    var maxTemp: IntObservableField = IntObservableField(38),
    var worryTemp: IntObservableField = IntObservableField(35),
    var time0: IntObservableField = IntObservableField(0),
    var time2: IntObservableField = IntObservableField(0),
    var t01064List: ArrayList<CalibrationBean> = getDefList(10),
    var t0532List: ArrayList<CalibrationBean> = getDefList(5),
    var t21064List: ArrayList<CalibrationBean> = getDefList(10),
    var t2532List: ArrayList<CalibrationBean> = getDefList(5),
    var maxUseCount: IntObservableField = IntObservableField(999999),
    var surUseCount: IntObservableField = IntObservableField(0),
    var jiaozhunxishu: FloatObservableField = FloatObservableField(1f),
    var lvxingTime: StringObservableField = StringObservableField(""),
    var lvxingTimeLong: Long = 0L,
    var lvxingRemainingTime: StringObservableField = StringObservableField("3")
) {
    var hastime0: Boolean = true
    var hastime2: Boolean = false

    val lvxingRemainingTimeSum: Long
        get() =
            lvxingTimeLong + lvxingRemainingTime.get().toLong().times(30 * 24 * 60 * 60 * 1000L)

    fun getLvXinReamin(): String {
        //剩余时间
        return getTimeFormat(lvxingRemainingTimeSum - Calendar.getInstance().timeInMillis)
    }

    fun getLvXinUse(): String {
        //剩余时间
        return getTimeFormat(Calendar.getInstance().timeInMillis - lvxingTimeLong)
    }

    fun getTimeFormat(time: Long): String {
        val day = time.div(24 * 60 * 60 * 1000)
        val hour = time.div(60 * 60 * 1000).rem(24)
        return "$day 天 $hour 时"
    }

    fun getDefTimeTitle(selectPS: Boolean): String {
        return if (selectPS) {
            getPimiaoTitle()
        } else {
            getNamiaoTitle()
        }
    }

    fun setMaxUseCount(count: Int) {
        maxUseCount.set(count)
    }

    fun getPimiaoTitle(): String {
        return if (pimiao300) "300 PS" else "500 PS"
    }

    fun getNamiaoTitle(): String {
        return if (namiao2) "2 NS" else "3 NS"
    }

    fun getMaikuan(isPs: Boolean): Int {
        return when {
            isPs.and(pimiao300) -> 300
            isPs.and(pimiao300).not() -> 500
            !isPs.and(namiao2) -> 2000
            else -> 3000
        }
    }

    fun getRealMode(): Int {
        return if (hastime2) {
            if (hastime0) {
                when {
                    time0.get() < 0 && time2.get() > 0 -> 0
                    time0.get() < 0 && time2.get() < 0 && time0.get() <= time2.get() -> 1
                    time0.get() > 0 && time2.get() > 0 && time0.get() <= time2.get() -> 2
                    time0.get() > 0 && time2.get() > 0 && time0.get() > time2.get() -> 3
                    time0.get() < 0 && time2.get() < 0 && time0.get() > time2.get() -> 4
                    else -> 5
                }
            } else {
                when {
                    time2.get() > 0 -> 8
                    else -> 9
                }
            }

        } else {
            when {
                time0.get() > 0 -> 6
                else -> 7
            }

        }

    }

    fun getRealTime(type: Int): Int {
        return when (type) {
            0 -> {
                if (hastime0)
                    time0.get().absoluteValue
                else
                    0
            }
            else -> {
                if (hastime2)
                    time2.get().absoluteValue
                else
                    0
            }
        }

    }

    fun getRealVolt(type: Int, mJ: Int): Int {
        return when (type) {
            0 -> {
                if (psOrNs.get() != 0b0010)
                    t01064List
                else
                    return 0
            }
            1 -> {
                if (psOrNs.get() != 0b0010)
                    t0532List
                else
                    return 0
            }
            2 -> {
                if (psOrNs.get() != 0b0001)
                    t21064List
                else
                    return 0
            }
            else -> {
                if (psOrNs.get() != 0b0001)
                    t2532List
                else
                    return 0
            }
        }.find {
            it.mJ == mJ
        }?.vol ?: 0

    }

    fun getRealHighestHz(): Int {
        return highestHz.get() + 5
    }

    fun setPsOrNs(i: Int) {
        if (i <= 0b0010) {
            hastime0 = i < 0b0010
            hastime2 = !hastime0
        }
        psOrNs.set(i)
    }

    fun add(type: Int) {
        when (type) {
            0 -> {
                if (minVoltage.get() < maxVoltage.get() - 50)
                    minVoltage.plus(5)
            }
            1 -> {
                if (maxVoltage.get() < 1000)
                    maxVoltage.plus(5)
            }
            2 -> {
                if (worryTemp.get() < maxTemp.get())
                    worryTemp.plus(1)
            }
            3 -> {
                if (minTemp.get() < maxTemp.get() - 1)
                    minTemp.plus(1)
            }
            4 -> {
                if (maxTemp.get() < 38)
                    maxTemp.plus(1)
            }
        }
    }

    fun minus(type: Int) {
        when (type) {
            0 -> {
                if (minVoltage.get() > 300)
                    minVoltage.minus(5)
            }
            1 -> {
                if (maxVoltage.get() > minVoltage.get() + 50)
                    maxVoltage.minus(5)
            }
            2 -> {
                if (worryTemp.get() > minTemp.get())
                    worryTemp.minus(1)
            }
            3 -> {
                if (minTemp.get() > 22)
                    minTemp.minus(1)
            }
            4 -> {
                if (maxTemp.get() > minTemp.get() + 1)
                    maxTemp.minus(1)
            }
        }
    }

    fun addJiaozhunxishu() {
        if (jiaozhunxishu.get() < 1f)
            jiaozhunxishu.set(
                jiaozhunxishu.get().toBigDecimal().setScale(1).plus(0.1.toBigDecimal()).toFloat()
            )
    }

    fun miusJiaozhunxishu() {
        if (jiaozhunxishu.get() > 0)
            jiaozhunxishu.set(
                jiaozhunxishu.get().toBigDecimal().setScale(1).minus(0.1.toBigDecimal()).toFloat()
            )
    }

    fun addTime(type: Int) {
        when (type) {
            0 -> {
                time0.takeIf {
                    it.get() < 250
                }
            }
            else -> time2.takeIf {
                it.get() < 250
            }
        }?.plus(1)
    }

    fun minusTime(type: Int) {
        when (type) {
            0 -> {
                time0.takeIf {
                    it.get() > -250
                }
            }
            else -> time2.takeIf {
                it.get() > -250
            }
        }?.minus(1)
    }
}

fun setMJAvg(data: List<CalibrationBean>) {
    val first = data.first()
    val last = data.last()
    val avg = (first.vol - last.vol).toFloat().div(data.lastIndex)
    for (index in data.lastIndex.downTo(2)) {
        val calibrationBean = data[data.size - index]
        calibrationBean.vol = (last.vol + avg * (index - 1)).toInt()
    }
}

fun getDefList(max: Int): ArrayList<CalibrationBean> {
    return ArrayList<CalibrationBean>(max).apply {
        for (i in max.downTo(1)) {
            add(CalibrationBean(50 * i, 400 + 50 * (i - 1)))
        }
    }
}

